<?php

/*
 * Class teia\webdev\php\core\ShoppingCart
* Core class for electronic shopping cart support
*
* @author Bernardo Quibiana
* @date created: 20111011
*
* @date modified: 20111011
* @modified: Bernardo Quibiana
*/

// namespace declaration: Abstract Data Types
namespace teia\webdev\php\core;

require_once 'IDisplayableItem.inc';
require_once 'ShoppingCartItem.inc';

class ShoppingCart implements IDisplayableItem {

	protected $cart_items;

	public function __construct(){
		$this->cart_items = array();
	}

	public function add_cart_item($prod_id, $item_id, $cart_qty, $item_link) {
		$cart_it_id = self::get_cart_it_id($prod_id, $item_id);
		if (isset($this->cart_items[$cart_it_id])) {
			$cart_it = $this->cart_items[$cart_it_id];
			$cart_it->add_cart_qty($cart_qty);
		} else {
			$cart_it = new ShoppingCartItem($prod_id, $item_id, $cart_qty, $item_link);
			//$this->cart_items[$cart_it_id] = $cart_it;
			$this->cart_items = array_merge(array($cart_it_id => $cart_it), $this->cart_items);
			//echo "added $prod_id-$item_id x $cart_qty <br />";
		}
	}

	public function remove_cart_item($prod_id, $item_id) {
		$cart_it_id = self::get_cart_it_id($prod_id, $item_id);
		if (isset($this->cart_items[$cart_it_id])) {
			unset($this->cart_items[$cart_it_id]);
		}
	}

	public function update_cart_item($prod_id, $item_id, $cart_qty) {
		$cart_it_id = self::get_cart_it_id($prod_id, $item_id);
		if (isset($this->cart_items[$cart_it_id])) {
			$cart_it = $this->cart_items[$cart_it_id];
			$cart_it->set_cart_qty($cart_qty);
		}
	}

	public function get_size() {
		$count = 0;
		foreach ($this->cart_items as $cart_it) {
			$count += $cart_it->get_cart_qty();
		}
		return $count;
	}
	
	public function get_subtotal() {
		$subtotal = 0.0;
		foreach ($this->cart_items as $cart_it) {
			$subtotal += $cart_it->get_subtotal();
		}
		return $subtotal;
	}

	public function get_prod_values($property) {
		$all_values = array();
		foreach ($this->cart_items as $cart_it) {
			$value = $cart_it->get_prod_property($property);
			array_push($all_values, $value);
		}
		sort($all_values);
		return array_unique($all_values);
	}
	
	public function get_items_values($property) {
		$all_values = array();
		foreach ($this->cart_items as $cart_it) {
			$value = $cart_it->get_item_property($property);
			array_push($all_values, $value);
		}
		sort($all_values);
		return array_unique($all_values);
	}
	
	public function get_filter_values($fields) {
		$values = array('prod_filter' => array(), 'item_filter' => array());
		foreach ($fields['prod_filter'] as $field) {
			$val = $this->get_prod_values($field);
			$values['prod_filter'][$field] = FunctionPool::array_single($val);
		}
		foreach ($fields['item_filter'] as $field) {
			$val =  $this->get_items_values($field);
			$values['item_filter'][$field] = FunctionPool::array_single($val);
		}
		return $values;
	}
	
	static function get_cart_it_id($prod_id, $item_id) {
		return $prod_id . '_' . $item_id;
	}

	/*
	 * IDisplayableItem
	*/
	public function get_thumbnail() {

	}


	public function get_slide_image() {
		return null;
	}


	public function get_small_image() {
		return null;
	}


	public function get_medium_image() {

	}

	public function get_large_image(){

	}


	public function get_alt_text() {

	}


	public function get_element_id() {

	}

	public function get_element_class() {

	}

	public function get_details() {

	}


	public function generate($context) {
		$content = "<div class=\"cart_content\">\n";
		$subtotal = 0.0;
		
		$content .= "<h2>Carrinho de compras</h2><hr />";
		if (empty($this->cart_items)) {
			// ecom.marketing.empty_cart_msg
			$content .= "<h3>Your shopping cart is empty. Feel free to do some shopping!</h3>";
		} else {
			foreach ($this->cart_items as $cart_it) {
				$content .= $cart_it->generate('page');
				$subtotal += $cart_it->get_subtotal();
				$content .= "<br /><hr />\n";
			}
		}

		$content .= "</div>";

		return $content;
	}

	public function display($context) {
		echo $this->generate($context);
	}
}

?>